// pages/video_comment/video_comment.js
const app = getApp()
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentList:[],
    isFocus:false,
    isReply:false,
    page:2,
    isReplys:false,
    reply_holder:'说点什么',
    commentStr:'',//评论内容
    type:1,//回复层级 默认为1 最大为2
    v_r_id:0,//回复上级id
    nofocus:true,
    addStatus:false,
    // emojs:[
    //   ['😀','😁','😂','🤣','😃','😄','😅','😆'],
    //   ['😉','😊','😋','🤣','😎','😍','😘','😗'],
    //   ['😓','😒','🤤','😝','😜','😛','😌','😴'],
    //   ['😫','😪','😯','🤐','😥','😮','😥','😣'],
    //   ['😏','🙄','😶','😐','🤔','🤗','🙂','😚'],
    //   ['😙','😀','😔','😕','🙃','🤑','😲','☹'],
    //   ['🙁','😖','😞','😟','😤','😢','😭','😦'],
    //   ['😧','😨','😩','😬','😰','😱','😳','😵'],
    //   ['😡','😠','😷','🤒','🤕','🤢','🤧','😇'],
    //   ['🤠','🤡','🤥','🤓','😈','👿','👹','👺'],
    //   ['💀','👻','👽','🤖','💩','😺','😸','😹'],
    //   ['😻','😼','😽','🙀','😿','😾','🏻','🏼'],
    //   ['🏽','🏾','🏿','🗣','👤','👤','👫','👬'],
    //   ['👭','👂','👂🏻','👂🏼','👂🏽','👂🏾','👂🏿','👃'],
    //   ['👃🏻','👃🏼','👃🏽','👃🏾','👃🏿','👣','👀','👁'],
    //   ['👅','👄','💋','👓','🕶','👔','👕','👖'],
    //   ['👗','👘','👙','👚','👛','👜','👝','🎒'],
    //   ['👞','👟','👠','👡','👢','👑','👒','🎩'],
    //   ['🎓','⛑','💄','💍','🌂','💼']
    // ],
    emojiChar: "☺-😋-😌-😍-😏-😜-😝-😞-😔-😪-😭-😁-😂-😃-😅-😆-👿-😒-😓-😔-😏-😖-😘-😚-😒-😡-😢-😣-😤-😢-😨-😳-😵-😷-😸-😻-😼-😽-😾-😿-🙊-🙋-🙏-✈-🚇-🚃-🚌-🍄-🍅-🍆-🍇-🍈-🍉-🍑-🍒-🍓-🐔-🐶-🐷-👦-👧-👱-👩-👰-👨-👲-👳-💃-💄-💅-💆-💇-🌹-💑-💓-💘-🚲",
    //0x1f---
    emoji: [
      "60a", "60b", "60c", "60d", "60f",
      "61b", "61d", "61e", "61f",
      "62a", "62c", "62e",
      "602", "603", "605", "606", "608",
      "612", "613", "614", "615", "616", "618", "619", "620", "621", "623", "624", "625", "627", "629", "633", "635", "637",
      "63a", "63b", "63c", "63d", "63e", "63f",
      "64a", "64b", "64f", "681",
      "68a", "68b", "68c",
      "344", "345", "346", "347", "348", "349", "351", "352", "353",
      "414", "415", "416",
      "466", "467", "468", "469", "470", "471", "472", "473",
      "483", "484", "485", "486", "487", "490", "491", "493", "498", "6b4"
    ],
    emojis:[],
    showEmoji:false,//控制表情盒子是否显示
    isLoad: true,//解决初次加载时emoji动画执行一次
  },
  // 输入框确认事件
  addFirstCom(isReply,isReplys,type,v_r_id){
    // 如果是回复状态
    isReply = this.data.isReply
    isReplys = this.data.isReplys
    if(isReply){
      let type = this.data.reply1_type
      let v_r_id = this.data.reply1_v_r_id
      this.addComment(type,v_r_id)
    }else if(isReplys){
      let type = this.data.reply2_type
      let v_r_id = this.data.reply2_v_r_id
      this.addComment(type,v_r_id)
    }else{
      let type = 1
      let v_r_id = 1
      this.addComment(type,v_r_id)
    }
  },
  // 添加评论
  addComment(type,v_r_id){
    if(this.data.commentStr==''){
      wx.showToast({
        title: '评论内容不能为空',
        icon:'none'
      })
      return
    }
    const that = this
    let file_id = that.data.file_id
    let content=that.data.commentStr
    let token = wx.getStorageSync('token')
    let username=that.data.userInfo.nickName
    let user_img=that.data.userInfo.avatarUrl
    wx.request({
      url: app.globalData.host+'Video/add_comment',
      method:'POST',
      data:{
        file_id,
        type,
        v_r_id,
        content,
        username,
        user_img,
        token,
      },
      success(res){
        that.setData({
          isFocus:false,
          isReply:false,
          isReplys:false,
          commentStr:'',
          reply_holder:'说点什么',
          addStatus:true
        })
        if(res.data.code==2000){
          wx.showToast({
            title: '添加评论成功',
            icon:'none'
          })
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
        that.getCommentList(1)
      }
    })
  },
  
  // 回复1级评论
  reply1(e){
    console.log(e.currentTarget.dataset)
    let u_name = e.currentTarget.dataset.name==''||null?this.data.uuid:e.currentTarget.dataset.name
    let type=2
    let v_r_id = e.currentTarget.dataset.id
    this.setData({
      isFocus:true,
      isReply:true,
      reply1_type:type,
      reply1_v_r_id:v_r_id,
      reply_holder:'回复@'+u_name
    })
  },
  // 回复2级评论
  reply2(e){
    console.log(e.currentTarget.dataset)
    let type=2
    let v_r_id = e.currentTarget.dataset.id
    let u_name = e.currentTarget.dataset.name==''||null?this.data.uuid:e.currentTarget.dataset.name
    this.setData({
      isFocus:true,
      isReply:false,
      isReplys:true,
      reply2_type:type,
      reply2_v_r_id:v_r_id,
      reply_holder:'回复@'+u_name
    })
  },
  // // 获取所有回复评论的条数
  // getLength(){
  //   let length1 = this.data.commentList
  // },
  // 输入/提交评论
  setStr(e){

    let val = e.detail.value
    let commentStr = this.data.commentStr
    this.setData({
      commentStr:val.replace(/\s+/g,'')
    })
  },
  // 输入框获取焦点
  foucsup(){
    this.setData({
      isFocus:true,
      isReply:false,
      s_h_emjios:false
    })
  },
  // 获取评论
  getCommentList(page){
    const that = this
    if(that.data.addStatus){
      page=1
      that.setData({
        page:1
      })
    }else{
      page = that.data.page
    }
    let file_id = that.data.file_id
    wx.request({
      url: app.globalData.host+'Video/comment_list',
      method:'POST',
      data:{
        file_id,
        page
      },
      success(res){
        console.log(res)
        let commentList = that.data.commentList
        let commentList1 = res.data.data
        if(commentList1==''){
          wx.showToast({
            title: '没有更多评论了',
            icon:'none'
          })
          return
        }
        if(page==1){
          commentList = res.data.data
        }else{
          commentList = [...commentList,...commentList1]
        }
        if(commentList==''){
          that.setData({
            noComments:true
          })
        }
        for(var index in commentList1){
          var commentTime = new Date(commentList1[index].create_time*1000).dateFormat('Y年m月d日')
          commentList1[index].create_time = commentTime
        }
        if(that.data.addStatus){
          page = that.data.page
        }else{
          page++
        }
        that.setData({
          commentList,
          commentStr:'',
          noComments:false,
          page,
          addStatus:false
        })
        
      },
      fail(err){
        console.log(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    that.setData({
      file_id:options.file_id
    })
    that.getCommentList(1)
    wx.getUserInfo({
      success: res => {
        var userInfo = res.userInfo
        this.setData({
          userInfo
        })
      }
    })
    let token = wx.getStorageSync('token')
    let uuid = '游客'+token.slice(5,10)
    that.setData({uuid})
    // emoji
    var em = {} 
    let emChar = that.data.emojiChar.split("-")
    that.data.emoji.forEach(function (v, i) {
      em = {
        char: emChar[i],
        emoji: "0x1f" + v
      }
      that.data.emojis.push(em)
      that.setData({
        emojis: that.data.emojis
      })
    })
  },
  // 控制表情盒子的显示隐藏
  s_h_emjios(){
    if(showEmoji){
      this.setData({
        showEmoji:false,
        isLoad: false,
      })
    }else{
      this.setData({
        showEmoji:true
      })
    }
  },
  //表情选择
  emojiChoose: function (e) {
    //当前输入内容和表情合并
    this.setData({
      content: this.data.content + e.currentTarget.dataset.emoji
    })
  },
  //解决滑动穿透问题
  emojiScroll: function (e) {
    console.log(e)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getCommentList(this.data.page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})